﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.FireProjects.BLL;
using Yipusoft.FireProjects.Model;

public partial class BasicBase_Drilling_DrillingList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        BindDepartment1();
        BindDepartment();
        DepartmentListBind();
        BindType();
        BindDDlClassid();
        BindFireProject();
    }

    private void BindDepartment()
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        UnitList.Items.Clear();
        UnitList.Items.Add(new ListItem("------------部门-----------------------", ""));
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {

            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }
        else
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }
    }

    private void BindType()
    {
        string strWhere = " ParentID='MHJYYALB0000' ";
        List<CodeItemInfo> list = CodeItem.GetList(strWhere);
        ddlTypeID.DataTextField = "ItemName";
        ddlTypeID.DataValueField = "ItemID";
        ddlTypeID.DataSource = list;
        ddlTypeID.DataBind();
    }


    protected void ddlTypeID_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDlClassid();
        BindFireProject();
    }

    void BindDDlClassid()
    {
        ddlClassID.Items.Clear();
        ddlClassID.Items.Add("");
        if (ddlTypeID.SelectedValue != "")
        {
            List<CodeItemInfo> list = CodeItem.GetList(" ParentID='" + ddlTypeID.Text + "' ");
            ddlClassID.DataTextField = "ItemName";
            ddlClassID.DataValueField = "ItemID";
            ddlClassID.DataSource = list;
            ddlClassID.DataBind();
        }
    }
    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {

        DepartmentListBind();
        BindFireProject();

    }

    void DepartmentListBind()
    {
        if (DepartmentList.SelectedValue != "")
        {
            string Departmentid = DepartmentList.SelectedValue;
            string strWhere = "(departmentid like '" + Departmentid.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or fullName like '%指挥中心%'  and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%')  or fullName like '%司令%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警务%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警勤中队%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%战训%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%')";
            UnitList.DataSource = Department.GetList(strWhere);
            UnitList.DataBind();
        }
        else
        {
            UnitList.DataSource = "";
            UnitList.DataBind();
        }
    }

    void BindFireProject()
    {
        string where = "";
        if (UnitList.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " DepartmentID='" + UnitList.SelectedValue + "' ";
        }
        else
        {
            if (DepartmentList.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                where += " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
            }
        }
        if (ddlTypeID.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " TypeID='" + ddlTypeID.SelectedValue + "' ";
        }

        if (ddlClassID.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " ClassID='" + ddlClassID.SelectedValue + "' ";
        }
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) != "1300")
        {
            where += where == "" ? "" : " And ";
            where += " DepartmentID like '" + department.DepartmentID.Substring(0, 4) + "%' ";
        }

        int count = FireProject.GetCount(where);
        ddlProjectName.Items.Clear();
        ddlProjectName.Items.Add("");
        ddlProjectName.DataSource = FireProject.GetList(count, 0, where);
        ddlProjectName.DataBind();
    }
    protected void ddlClassID_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindFireProject();
    }



    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindFireProject();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        departmentListBind1();
    }

    private void BindDepartment1()
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {


            departmentList1.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
            departmentList1.DataBind();
            departmentListBind1();
        }
        else
        {
            departmentList1.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
            departmentList1.DataBind();
            departmentListBind1();
        }
    }

    private void departmentListBind1()
    {
        UnitList1.Items.Clear();
        UnitList1.Items.Add(new ListItem("------------部门-----------------------", ""));
        if (departmentList1.SelectedValue != "")
        {
            string Departmentid = departmentList1.SelectedValue;
            string strWhere = "(departmentid like '" + Departmentid.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or fullName like '%指挥中心%'  and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%')  or fullName like '%司令%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警务%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%警勤中队%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%') or fullName like '%战训%' and   (DepartmentID LIKE '" + Departmentid.Substring(0, 4) + "%')";

            UnitList1.DataSource = Department.GetList(strWhere);
            UnitList1.DataBind();
        }
        else
        {
            UnitList1.DataSource = null;
            UnitList1.DataBind();
        }
    }

    protected void lbtQurey_Click(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201312") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201315"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        ListPageContral1.SetFirstPage();
        Qurey(0);
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(ListPageContral1.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Qurey(PageIndex);
    }

    void Qurey(int PageIndex)
    {
        string where = "";
        if (ddlProjectName.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where = " FireProjectID='" + ddlProjectName.SelectedValue + "' ";
        }

        if (UnitList1.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " DepartmentId='" + UnitList1.SelectedValue + "' ";
        }
        else
        {
            if (departmentList1.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                where += " DepartmentId like '" + departmentList1.SelectedValue.Substring(0, 4) + "%' ";
            }
        }
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) != "1300")
        {
            where += where == "" ? "" : " And ";
            where += " DepartmentID like '" + department.DepartmentID.Substring(0, 4) + "%' ";
        }
        int count = DrillingNote.GetCount(where);
        List<DrillingNoteInfo> list = DrillingNote.GetList(ListPageContral1.PageSize, PageIndex, where);

        ListPageContral1.ItemCount = count;
        UserGrid.DataSource = list;
        UserGrid.DataBind();
    }

    protected string GetDepartmentName(string data)
    {
        return Department.GetDepartment(data).FullName;
    }

    protected string GetFireProjectName(string data)
    {
        FireProjectInfo info = FireProject.getFireproject(new Guid(data));
        return info == null ? "" : info.ProjectName;
    }
    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201312") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201318"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            if (DrillingNote.Delete(new Guid(e.CommandArgument.ToString())))
            {

                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "删除演练记录";
                log.ItemID = e.CommandArgument.ToString();
                log.ItemName = "Drilling";
                log.ItemFullUrl = "Drilling/DrillingNoteNew.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
                ListPageContral1.SetFirstPage();
                Qurey(0);
                Prompt.Text = "提示：删除成功！";
            }
            else
            {
                Prompt.Text = "提示：删除失败！";
            }
        }
    }
}
